package pers.cz.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

public class Postgirl implements UserDetails, Serializable {
    private Integer userId;

    private String userName;

    private Date createDate;

    private String passWord;

    private Date updateDate;

    private String enabled;

    private String accountExpired;

    private String locked;

    private String credentialsExpired;

    private List<Role> roles;

    private String realName;

    private String phoneId;

    private String email;

    private String verifyCode;

    public String getVerifyCode() {
        return verifyCode;
    }

    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPhoneId() {
        return phoneId;
    }

    public void setPhoneId(String phoneId) {
        this.phoneId = phoneId;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName == null ? null : userName.trim();
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord == null ? null : passWord.trim();
    }

    public Date getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

/*    public String getEnabled() {
        return enabled;
    }*/

    public void setEnabled(String enabled) {
        this.enabled = enabled == null ? null : enabled.trim();
    }

    public String getAccountExpired() {
        return accountExpired;
    }

    public void setAccountExpired(String accountExpired) {
        this.accountExpired = accountExpired == null ? null : accountExpired.trim();
    }

    public String getLocked() {
        return locked;
    }

    public void setLocked(String locked) {
        this.locked = locked == null ? null : locked.trim();
    }

    public String getCredentialsExpired() {
        return credentialsExpired;
    }

    public void setCredentialsExpired(String credentialsExpired) {
        this.credentialsExpired = credentialsExpired == null ? null : credentialsExpired.trim();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        for (Role role : roles) {
            list.add(new SimpleGrantedAuthority(role.getRoleName()));
        }
        return list;
    }

    @Override
    public String getPassword() {
        return this.passWord;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        if ("1".equals(this.accountExpired)){
            return true;
        }
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        if ("1".equals(this.locked)){
            return true;
        }
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        if ("1".equals(this.credentialsExpired)){
            return true;
        }
        return false;
    }

    @Override
    public boolean isEnabled() {
        // 防止修改最后登录时间时该状态为null,会返回一个fasle，导致用户被禁用。
        if (this.enabled == null) {
            return true;
        }
        if ("1".equals(this.enabled)){
            return true;
        }
        return false;
    }
}
